using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Acms.Bll;
using System.Linq;
using System.Collections.Generic;
/// <summary>
/// Summary description for BasePAge
/// </summary>
public abstract class BasePage : Acms.AcmsControls.TemplatePage
{
    protected override void OnInit(EventArgs e)
    {
        if (base.AcmsContext != null && base.AcmsContext.SiteId == 0)
        {
            //System.Text.StringBuilder tmp = new System.Text.StringBuilder();
            //tmp.Append(Request.ServerVariables["Server_Name"]);
            //tmp.Append(Request.ApplicationPath.Substring(0, Request.ApplicationPath.ToLower().IndexOf("/acms")));
            //AcmsSite = Acms.Bll.Site.GetByRootUrl(tmp.ToString());

            //if (AcmsSite == null)
            //{
            //    int siteId = 0;
            //    try
            //    {
            //        siteId = int.Parse(ConfigurationManager.AppSettings["SiteId"]);
            //    }
            //    catch (FormatException)
            //    {
             //   }
              //  catch (ArgumentNullException)
               // {
                //}

                /*if (siteId > 0)
                {
                    AcmsSite = Acms.Bll.Site.GetById(siteId);
                }*/
            //}

            //if (AcmsSite == null)
            //{
                if (!Page.Request.AppRelativeCurrentExecutionFilePath.Equals("~/Site/SetUp/NoSite.aspx", StringComparison.InvariantCultureIgnoreCase))
                {
                    Response.Redirect("~/Site/SetUp/NoSite.aspx", true);
                }
            //}

        }
        if (AcmsContext != null && !HasAdmin && !Page.Request.AppRelativeCurrentExecutionFilePath.Equals("~/Site/SetUp/NoSite.aspx", StringComparison.InvariantCultureIgnoreCase))
        {
            List<Acms.Bll.User> devs = Acms.Bll.User.GetAllByRoleAndSiteId(Role.Roles.Developer, AcmsContext.SiteId);
            if (devs.Count > 0)
            {
                hasAdmin = true;
            }
            else if (!Page.Request.AppRelativeCurrentExecutionFilePath.Equals("~/Site/SetUp/NoAdmin.aspx", StringComparison.InvariantCultureIgnoreCase))
            {
                Response.Redirect("~/Site/SetUp/NoAdmin.aspx", true);
            }
        }
        EnsureAccess();
        base.OnInit(e);
    }

    private static bool hasAdmin;
    public static bool HasAdmin
    {
        get { return hasAdmin; }
        set { hasAdmin = value; }
    }

    /*private static Acms.Bll.Site acmsSite;
    public static Acms.Bll.Site AcmsSite
    {
        get 
        { 
            return acmsSite; 
        }
        set
        {
            if (value != null && value.Id >= 0)
            {
                acmsSite = value;
            }
        }
    }*/

    public abstract Acms.Bll.Role.Roles[] AllowedRoles{get;}
    public virtual void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs) { }

    private void EnsureAccess()
    {
        bool allowAccess = false;
        if (User == null || User.Identity == null || !User.Identity.IsAuthenticated)
        {
            allowAccess = AllowedRoles.Contains(Acms.Bll.Role.Roles.Anonomous);
        }
        else
        {
            allowAccess = User.HasRole(AllowedRoles);
        }

        if (!allowAccess)
        {
            if (User == null || User.Identity == null || !User.Identity.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            else
            {
                Server.Transfer("~/IncorrectRights.aspx");
            }
        }
    }

    public new Acms.Bll.UserPrinciple User
    {
        get
        {
            return base.User as Acms.Bll.UserPrinciple;
        }
    }

    protected override object SaveViewState()
    {
        ViewState["PageTitle"] = Title;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        Title = ViewState["PageTitle"] as string;
    }

    //public string GetSiteRoot()
    //{
        /*if (!string.IsNullOrEmpty(ConfigurationSettings.AppSettings["PreviewRoot"]))
        {
            return ConfigurationSettings.AppSettings["PreviewRoot"];
        }
        else if (Page.Request.ApplicationPath.ToLower().EndsWith("/acms"))
        {
            System.Text.StringBuilder tmpUrl = new System.Text.StringBuilder();
            tmpUrl.Append("http://");
            tmpUrl.Append(Page.Request.ServerVariables["Server_Name"]);
            tmpUrl.Append(Page.Request.ApplicationPath.Substring(0, Page.Request.ApplicationPath.Length - 4));

            return tmpUrl.ToString();
        }
        else
        {
            return string.Concat("http://", BasePage.AcmsSite.Root);
        }*/
        //return AcmsContext.SiteRoot;
    //}
}
